package com.phkj.qdh.ui.fragment.myfailover

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.phkj.qdh.R
import com.phkj.qdh.databinding.FragmentMyFailoverBaseDetailBinding
import com.phkj.qdh.databinding.FragmentMyLawNoticeDetailBinding
import com.phkj.qdh.ui.adapter.BaseInspectDetailAdapter
import com.phkj.qdh.ui.adapter.ImagerViewAdapter
import com.phkj.qdh.ui.fragment.user.UserVM
import com.phkj.qdh.ui.fragment.user.getUserVMProvider
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import kotlinx.coroutines.flow.collectLatest

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 容错 基础巡查详情
 */
class MyFailoverBaseDetailFragment : Fragment() {
    private var id: Int? = null
    private var param2: String? = null
    private var _binding: FragmentMyFailoverBaseDetailBinding? = null
    private val binding get() = _binding!!
    private val iVAdapter by lazy { ImagerViewAdapter(true) }
    private val baseInspectDetailAdapter by lazy { BaseInspectDetailAdapter() }
    private lateinit var vm: UserVM
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            id = it.getInt(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentMyFailoverBaseDetailBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getUserVMProvider())[UserVM::class.java]
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack()
            }
            rvDetail.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = baseInspectDetailAdapter
            }
            rvBaseSubmit.run {
                layoutManager =
                    LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
                addItemDecoration(
                    MyItemDecoration(
                        ContextCompat.getColor(requireContext(), R.color.transparent),
                        28,
                        RecyclerView.HORIZONTAL
                    )
                )
                adapter = iVAdapter
            }
        }

        launchAndRepeatWithViewLifecycle {
            vm.baseDetail.collectLatest {
                binding.run {
                    tvSort1.text = it.keyPointTypeName
                    tvSecond.text = it.lastKeyPointTypeName
                    baseInspectDetailAdapter.submitList(it.keyPointDataList)
                    tvRisk.text = if (it.anyRisk == 0) "有" else "无"
                    tvState.text = if (it.rectification == 0) "待整改" else "已整改"
                    tvMingjing.text  = it.checkUsers.toString().replace("[","").replace(","," ").replace("]","")
                    edtRemark.text  = it.remark
                    btnOk.text =     when (it.state) {
                        1 -> {"待下发"}
                        2 -> {"待处理"}
                        3 -> {"已反馈"}
                        4 -> {"已驳回"}
                        5 -> {"已退回"}
                        else -> {"已完成"}
                    }
                    tvBanFa.text = it.policeName
                    tvFen.text = it.policeScore.toString()
                }
                iVAdapter.submitList(it.imageUrl.split(","))

            }
        }

        vm.getBaseDetail(id!!)
    }

    companion object {
        @JvmStatic
        fun newInstance(id: Int, param2: String) =
            MyFailoverBaseDetailFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PARAM1, id)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}